#include "stdafx.h"

#include "LandMap_common.h"

#include <osgText/Text>

void findEveryGeode(Node* node, vector<Geode*>& geodeList)
{
	Geode* geode = dynamic_cast<Geode*>(node);
	if (geode)
	{
		int size = geodeList.size();
		for (int i = 0; i < size; ++i)
		{
			if (geodeList[i] == geode)
			{
				return;
			}
		}
		geodeList.push_back(geode);
		return;
	}

	Group* group = dynamic_cast<Group*>(node);
	if (group)
	{
		int num = group->getNumChildren();
		for (int i = 0; i < num; ++i)
		{
			findEveryGeode(group->getChild(i), geodeList);
		}
	}
}

void reprojectOneGeode( Geode* geode )
{
	MatrixList matrixList = geode->getWorldMatrices();
	Matrixd worldMatrix = matrixList[0];

	const Geode::DrawableList& drawList = geode->getDrawableList();
	Geode::DrawableList::const_iterator iter = drawList.begin();
	for (; iter != drawList.end(); ++iter)
	{
		Geometry* geom = dynamic_cast<Geometry*>((*iter).get());
		if (geom)
		{
			Vec3Array& vertex = static_cast<Vec3Array&>(*geom->getVertexArray());
			for (unsigned i = 0; i < vertex.size(); ++i)
			{
				double x, y;
				Vec3d tmpVertex = worldMatrix.preMult(vertex[i]);
				SceneCenter::inst()->LLToXY(tmpVertex.x(), tmpVertex.y(), x, y);
				vertex[i].x() = x;
				vertex[i].y() = y;
			}
			Vec4Array& color = static_cast<Vec4Array&>(*geom->getColorArray());
			Vec4 oneColor = color[0];
			color.clear();
			color.push_back(oneColor);
			geom->setColorBinding(Geometry::BIND_OVERALL);

			geom->dirtyBound();

			continue;
		}

		osgText::Text* text = dynamic_cast<osgText::Text*>((*iter).get());
		if (text)
		{
			double x, y;
			Vec3d pos = text->getPosition();
			Vec3d tmpPos = worldMatrix.preMult(pos);
			SceneCenter::inst()->LLToXY(tmpPos.x(), tmpPos.y(), x, y);
			text->setPosition(Vec3(x, y, pos.z()));
			text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
			text->setFont("font/simsun.ttc");

			text->dirtyBound();
		}
		
	}

}

void reprojectEveryGeode(vector<Geode*>& geodeList)
{
	int size = geodeList.size();
	for (int i = 0; i < size; ++i)
	{
		reprojectOneGeode(geodeList[i]);
	}
}